﻿using Mbs.Elements;

namespace Mbs.ComponentModel
{
    /// <summary>
    /// Copy paste action base class
    /// </summary>
    public abstract class CopyAction : ICopyAction
    {
        protected static IElementCommand PasteCommand { get; private set; }
        protected static IElementCommand CutCommand { get; private set; }

        static CopyAction()
        {
            PasteCommand = SystemCommands.Paste.Command();
            CutCommand = SystemCommands.Cut.Command();
        }

        /// <summary>
        /// copy
        /// </summary>
        public void Copy()
        {
            if (CanCopy)
                PasteCommand.Enabled = CutCommand.Enabled = DoCopy();
        }

        /// <summary>
        /// paste
        /// </summary>
        public void Paste()
        {
            if (CanPaste)
            {
                UndoManager.Current.BeginTransaction("Paste");
                if (DoPaste())
                    UndoManager.Current.Commit();
                else
                    UndoManager.Current.Rollback();
            }
        }

        public void Cut()
        {
            if (CanCut)
            {
                UndoManager.Current.BeginTransaction("Cut");
                if (DoCut())
                    UndoManager.Current.Commit();
                else
                    UndoManager.Current.Rollback();
            }
        }
        /// <summary>
        /// do copy
        /// </summary>
        /// <returns></returns>
        protected abstract bool DoCopy();
        /// <summary>
        /// can copy
        /// </summary>
        public abstract bool CanCopy { get; }
        /// <summary>
        /// can paste
        /// </summary>
        public abstract bool CanPaste { get; }
        /// <summary>
        /// do paste
        /// </summary>
        /// <returns></returns>
        protected abstract bool DoPaste();
        /// <summary>
        /// can cut
        /// </summary>
        public abstract bool CanCut { get; }
        /// <summary>
        /// do cut
        /// </summary>
        /// <returns></returns>
        protected abstract bool DoCut();

        private static CopyAction _current;
        public static CopyAction Current
        {
            get { return _current; }
            set
            {
                if (value == null)
                    PasteCommand.Enabled = CutCommand.Enabled = false;
                else if (value != _current)
                {
                    _current = value;
                    PasteCommand.Enabled = _current.CanPaste;
                    CutCommand.Enabled = _current.CanCut;
                        
                }
            }
        }
    }

}
